
#include "C:\SAFIRE\Y\Y_BIFn\Y_BIFn.y.h"

#include "C:\SAFIRE\Y\PH_SL.y.h"

#include "C:\SAFIRE\Y\Mgt_SL.y.h"

#include "C:\SAFIRE\Y\ConfigUdp_TP.y.h"

#include "C:\SAFIRE\Users\SIP_USER\ws\SIP\source\Common_LIB\Scenario_HTML_Gen.y.h"

#include "C:\SAFIRE\USERS\SIP_USER\ws\SIP\source\SIP_LIB\SIP_LIB.y.h"

#include "C:\SAFIRE\USERS\SIP_USER\ws\SIP\source\SIP_T_LIB\SIP_T_LIB.y.h"


typedef SIP_T_ST_CC_TE_CE_V [SIP_Signals_T SIP_G, PH_SL UDP_Data_G, Mgt_SL UDP_Mgt_G]
{
            
    TIMER SIGNAL        TSupervisor();
    TIMER SIGNAL        TAct_Rq();
    TIMER SIGNAL        TResponse();
    TIMER SIGNAL        TStart();
    TIMER SIGNAL        T_Wait_Autoanswer();
    TIMER SIGNAL        T_Msg_Timer();
	
       
    Request_T           Request_TX_1;
    Request_T           Request_TX_2;
    Request_T           Request_RX_1;
    Request_T           Request_RX_2;
    
    Response_T          Response_T_TX_1;
    Response_T          Response_T_TX_2;
    Response_T          Response_T_RX_1;
    Response_T          Response_T_RX_2;
    
    Request_Line_T      Request_Line;
    Message_Headers_T   Message_Headers;
    Message_Body_T      Message_Body;
    
    PH_SL               PH_SL_S;
    SIP_Signal_T        SIP_Signal;    
    Config_UDP_T        ConfigUDP;
    
    STRING              CRLF;
        
    STRING              ScenarioName;
    STRING              ScenarioDecription;
    STRING              HTML_Title;
    STRING              ScenarioVerdict;
    STRING              ScenarioReason;    
    STRING              HTMLFilePath;
    STRING              Mode;
    STRING              Comment;
    NUMBER              HTML_File;
	
	SIP_T_Body_T		SIP_T_Body_IAM;
	SIP_T_Body_T		SIP_T_Body_ACM;	
	SIP_T_Body_T		SIP_T_Body_ANM;
	SIP_T_Body_T		SIP_T_Body_RLC;	
	SIP_T_Body_T		SIP_T_Body_REL;	
	
	SIP_Addr_Spec_T		SUT_Addr;
	SIP_Addr_Spec_T		Tester_Addr;
	
  
    SUT_Addr.SIP_URI.UserInfo.User = "228";
	SUT_Addr.SIP_URI.UserInfo.Password = NULL;		
	SUT_Addr.SIP_URI.HostPort.Host = "10.0.1.37";
	SUT_Addr.SIP_URI.HostPort.Port = 5060;
    SUT_Addr.SIP_URI.URI_Parameters = NULL;
	SUT_Addr.SIP_URI.URI_Headers = NULL;
	
    Tester_Addr.SIP_URI.UserInfo.User = "1000";
	Tester_Addr.SIP_URI.UserInfo.Password = NULL;	
	Tester_Addr.SIP_URI.HostPort.Host = "10.0.1.30";
	Tester_Addr.SIP_URI.HostPort.Port = 5060;
	Tester_Addr.SIP_URI.URI_Parameters = NULL;
	Tester_Addr.SIP_URI.URI_Headers = NULL;
	
	
	Request_Line.Method.Invite.MustBeNull = NULL;
	Request_Line.Request_URI.Addr_Spec = SUT_Addr;
    Request_Line.SIP_Version = "SIP/2.0";
	
    Message_Headers.Call_Id.Callid.Token = "c3eF6f58-363323Cd-8058EB88-10303915";
    Message_Headers.Call_Id.Callid.Host = Tester_Addr.SIP_URI.HostPort.Host;
	Message_Headers.Contact.Contact_Spec.Contact_Param_List.Contact_Param.Addr.Name_Addr.Display_Name.As_Plain_String.Token_List = SUT_Addr.SIP_URI.UserInfo.User;
	Message_Headers.Contact.Contact_Spec.Contact_Param_List.Contact_Param.Addr.Name_Addr.Addr_Spec = Tester_Addr; 
	Message_Headers.CSeq.Cseq_Number = 1;
	Message_Headers.CSeq.Method.Invite.MustBeNull = NULL;
	Message_Headers.From.From_Spec.Addr.Name_Addr.Addr_Spec = Tester_Addr;
	Message_Headers.From.From_Spec.From_Param_List.Token = "tag=a3bhg";        
	Message_Headers.To.To_Spec.Addr.Addr_Spec = SUT_Addr;
	Message_Headers.Via.Via_Param_List.Via_Param.Sent_Protocol.Token = "SIP/2.0/UDP"; 
	Message_Headers.Via.Via_Param_List.Via_Param.Sent_by.HostPort.Host = Tester_Addr.SIP_URI.HostPort.Host;
	Message_Headers.Via.Via_Param_List.Via_Param.Sent_by.HostPort.Port = Tester_Addr.SIP_URI.HostPort.Port;
	Message_Headers.Via.Via_Param_List.Via_Param.Via_Params_List.Token = "branch=z9hG4bKkdjuw2212";	
	Message_Headers.Max_Forwards.Max_Forwards_Number = 70;	
	Message_Headers.Allow.Method_List.Token_List = "INVITE, ACK, BYE, CANCEL, REFER, OPTIONS, REGISTER, SUBSCRIBE";
	Message_Headers.Content_Type.Media_Type.Token_List = "application/sdp";
	Message_Headers.Content_Length.ContentLength = 1024;
	
	Print("SIP_ST_CE - Message Header is ----------------:");
	Print(Message_Headers);
	
	CRLF = ConvertFromHex("0a0d");   

    SIP_T_Body_IAM = ConvertFromHex( "1501010020010A00020A08819050608006000F0A08041779516028223000");
	SIP_T_Body_ACM = ConvertFromHex( "AA000616040129010100");
	SIP_T_Body_ANM = ConvertFromHex( "F0010900");
	SIP_T_Body_RLC = ConvertFromHex( "7D021000");
	SIP_T_Body_REL = ConvertFromHex( "75000C0200028090");

	Message_Body =  SIP_T_Body_IAM;
	
    Request_TX_1.Request_Line = 	Request_Line;    
    Request_TX_1.Message_Headers = 	Message_Headers;             
    Request_TX_1.Message_Body =  	Message_Body;          
        
    
    Mode= "ab";
    HTMLFilePath= "C:\\SAFIRE\\Temp\\SIP_T_ST_CC_TE_CE_V.html";
    
    HTML_File= OpenFile( HTMLFilePath,
                         Mode);
                             
    HTML_Title = "SIP_ST_CC_TE_CE_V";
    ScenarioName = "SIP_ST_CC_TE_CE_V";
    ScenarioDecription = "SUT shoud correclty process SIP-T payload";
    Scenario_Gen_HTML_Header( HTML_File,
                              HTML_Title,
                              ScenarioName,
                              ScenarioDecription);

    START TStart FOR 1000;
    Print("\n\nScenario_1 : ---  started");
    
    ? .TStart()
    {
         Print("\n\nScenario_1 : ---  started");
         :: Preamble;
    }        
     
    
    STATE Preamble
    {        
        Print("SIP_ST_CC_TE - Start UDP config");
        ConfigUDP.Src.IpAddress=        Tester_Addr.SIP_URI.HostPort.Host;
        ConfigUDP.Src.Port=             Tester_Addr.SIP_URI.HostPort.Port;
        ConfigUDP.Dst.IpAddress=        SUT_Addr.SIP_URI.HostPort.Host;
        ConfigUDP.Dst.Port=             SUT_Addr.SIP_URI.HostPort.Port;
        ConfigUDP.MaxPacketSize=        1024;
        Print("End UDP config");

        Print("SIP_ST_CC_TE - Configuring UDP");
        UDP_Mgt_G ! Mgt_SL.Configure( ConfigUDP);
        
        Print("SIP_ST_CC_TE - Activating UDP");
        UDP_Data_G ! PH_SL.PH_Act_Rq();
        START TAct_Rq FOR 10000;        
    
        UDP_Data_G ? PH_SL.PH_Act_Ind() : PH_SL_S 
        {
            Print( "SIP_ST_CC_TE - UPD Activated");
            START TSupervisor FOR 10000;
            
            Scenario_Gen_HTML_Verdict( HTML_File,
                                       0, 
                                       "PASS",
                                       "UDP activated");
            :: Starting;
        }
        
        UDP_Data_G ? PH_SL.PH_Deact_Ind() : PH_SL_S 
        {
            Print( "SIP_ST_CC_TE - UPD Act_Rq not successfull  ... PH_Deact_Ind received!");
            
            Scenario_Gen_HTML_Verdict( HTML_File,
                                       1, 
                                       "FAIL",
                                       "UPD Act_Rq not successfull  ... PH_Deact_Ind received!");            
            STOP;
        }
        
        ? .TAct_Rq()
        {
            Print( "SIP_ST_CC_TE - UPD Act_Req not successfull  ... Timeout!");
            Scenario_Gen_HTML_Verdict( HTML_File,
                                       1, 
                                       "FAIL",
                                       "UPD Act_Req not successfull  ... timeout!");
                                       
            UDP_Data_G ! PH_SL.PH_Deact_Rq();
            STOP;
        }                
    }             
     
    STATE Starting
    {
    
        Print("SIP_ST_CC_TE - Starting");

        SIP_G ! SIP_Signals_T.SIP_Invite (Request_TX_1);
        Print("SIP_ST_CC_TE - SIP_Invite message sent ... waiting for SIP_100_Trying");
        
        START TResponse FOR 5000;        
        :: Wait_Response;
    
    }

    STATE Wait_Response
    {        

    	SIP_G ? SIP_Signals_T.SIP_100_Trying (?) : SIP_Signal
        {
			Print("SIP_ST_CC_TE - - VERDICT: PASS! - SIP_100_Trying received");
			Scenario_Gen_HTML_Verdict( HTML_File, 0, "PASS", "SIP_100_Trying received"); 
            
            :: Wait_Response;
        }
    
        SIP_G ? SIP_Signals_T.SIP_180_Ringing (?) : SIP_Signal 
        {
            Print("SIP_ST_CC_TE - VERDICT: PASS! - SIP_180_Ringing received");
            Scenario_Gen_HTML_Verdict( HTML_File, 0, "PASS", "SIP_180_Ringing received"); 
            :: Wait_Response;
                  
        }
        
        SIP_G ? SIP_Signals_T.SIP_200_OK (?) : SIP_Signal 
        {
            Print("SIP_ST_CC_TE - VERDICT: PASS! - SIP_200_OK received");
            Scenario_Gen_HTML_Verdict( HTML_File, 1, "PASS", "SIP_200_OK received");
            
            SIP_G ! SIP_Signals_T.SIP_Ack (Request_TX_1);
            
            START T_Msg_Timer FOR 3000;
            
            :: Terminate_Call;
        
        }

		
		SIP_G ? SIP_Signals_T.SIP_Invalid(?) : SIP_Signal
		{
			Print("SIP_ST_CC_TE - VERDICT: FAIL! - Encoding Error");
            Print(SIP_Signals_T.SIP_Signal.ErrorDescription);
                                
            Scenario_Gen_HTML_Verdict( HTML_File, 1, "FAIL", "Encoding Error");
            :: Postamble;
		}
		
		SIP_G ? ? : SIP_Signal 
        {                
            Print("SIP_ST_CC_TE - VERDICT: FAIL! - Unexpected message received");
            Print(SIP_Signal);
            Scenario_Gen_HTML_Verdict( HTML_File, 1, "FAIL", "Unexpected message received");                                 
            :: Postamble;
        }
        
     
        ? .TResponse()
        {
            Print("SIP_ST_CC_TE - VERDICT: FAIL! - TResponse timer expiered");
                                           
            Scenario_Gen_HTML_Verdict( HTML_File,
                                       1, 
                                       "FAIL",
                                       "TResponse timer expiered");                                  
                                
            :: Postamble;
        }
        
        ? .TSupervisor()
        {
            Print("SIP_ST_CC_TE - VERDICT: FAIL! - TSupervisor timer expiered");
            Scenario_Gen_HTML_Verdict( HTML_File,
                                       1, 
                                       "FAIL",
                                       "TSupervisor timer expiered");
            
            :: Postamble;
        }
    } 
    
    STATE Terminate_Call
    {
        ? .T_Msg_Timer()
        {
            Request_TX_2 = Request_TX_1;            
            Request_TX_2.Request_Line.Method.Cancel.MustBeNull = NULL;
            Request_TX_2.Message_Headers.CSeq.Method.Cancel.MustBeNull = NULL;
            SIP_G ! SIP_Signals_T.SIP_Cancel (Request_TX_2);
            Print("SIP_Cancel sent");
            
            START T_Msg_Timer FOR 5000;
            :: Postamble;
        }
    }
    
    STATE Postamble
    {
        Print("SIP_ST_CC_TE - ... ending the test scenario");
        UDP_Data_G ! PH_SL.PH_Deact_Rq();
        Scenario_Gen_HTML_Footer( HTML_File);
        CloseFile( HTML_File);
        STOP;                
    }
};